Skip to main content

Utility Scripts

General-purpose utilities for working with geospatial raster data, including metadata generation, statistical analysis, and size calculations.

gdal2metadata.py

Generates FGDC (Federal Geographic Data Committee) metadata XML from GDAL-supported raster files.

Usage

Parameters

string
required
Input georeferenced raster file
string
required
Input FGDC metadata template XML file
string
required
Output populated FGDC metadata XML file

Options

flag
Print detailed image information during processing
flag
Compute and report min/max values
flag
Compute and report statistics
flag
Report histograms

Metadata Fields Populated

Automatically extracts and populates:

Spatial Information

  • Coordinate system - Projection name and parameters
  • Datum and spheroid - Target body, radii, flattening
  • Resolution - Pixel size in degrees or meters
  • Extent - Bounding coordinates (westbc, eastbc, northbc, southbc)
  • Image dimensions - Rows and columns

Projection Parameters

Supports and extracts parameters for:
  • Equirectangular
  • Mercator
  • Transverse Mercator
  • Sinusoidal
  • Robinson
  • Stereographic
  • Polar Stereographic
  • Orthographic

Technical Details

  • Data type and bit depth
  • NoData values
  • Scale and offset

Example

Template File

Requires an FGDC XML template with placeholder elements. The script populates:

Coordinate Normalization

Longitudes are automatically normalized to -180 to 180 range for FGDC validation compliance.

Requirements

Dependencies:
  • Python 2.7+ or Python 3.x
  • GDAL Python bindings
  • lxml or xml.etree for XML processing

gdal_hist.py

Exports raster histogram data in tab-delimited format for analysis and visualization.

Usage

Parameters

string
required
Input raster dataset

Options

flag
Compute and display min/max values
flag
Compute and display statistics (min, max, mean, stddev, RMS)
flag
Export histogram data (required)
flag
Apply scale and offset to unscale values to original units
At least one flag (-mm, -stats, or -hist) must be specified.

Output Format

Statistics Output

Histogram Output

Columns:
  • level - Bin number
  • value - Center value of bin
  • count - Number of pixels in bin
  • cumulative - Cumulative percentage (0-1)

Example

Scale and Offset

When -unscale is used:
Reads scale and offset from raster metadata.

Multi-band Files

For multi-band files, each band is processed separately:

slope_histogram_cumulative_graph.py

Creates histogram and cumulative distribution visualizations from tabular histogram data.

Usage

Parameters

string
required
Input tab-delimited histogram file (from gdal_hist.py)
string
required
Output PNG image file

Options

string
Title for the plot

Input File Format

Expects tab-delimited format from gdal_hist.py:

Output Visualization

Generates a dual-axis plot:
  • Left Y-axis - Frequency histogram (gray filled area)
  • Right Y-axis - Cumulative distribution (blue line)
  • X-axis - Value (typically slope in degrees)
  • Vertical line - Reference line at x=15 (suitable for slopes)

Example

Customization

The script can be modified to:
  • Change reference line position (default: x=15)
  • Adjust color scheme
  • Modify axis labels
  • Change figure size

Requirements

Dependencies:
  • Python 3.x
  • pandas
  • matplotlib

gdalSize.py

Calculates uncompressed raster file size based on geographic extent and resolution.

Usage

Parameters

float
required
Minimum longitude (degrees)
float
required
Minimum latitude (degrees)
float
required
Maximum longitude (degrees)
float
required
Maximum latitude (degrees)
float
required
Resolution in meters per pixel
integer
required
Bit depth: 8, 16, or 32
integer
required
Number of bands
string
required
Reference image for projection information

Output

Reports estimated file size:

How It Works

  1. Reads projection from reference image
  2. Transforms geographic bounds to projected coordinates
  3. Calculates dimensions based on resolution
  4. Computes uncompressed size: lines × samples × bands × bytes_per_pixel

Size Calculation

Example

Output:

Use Cases

  • Storage planning - Estimate disk space before processing
  • Data ordering - Calculate download sizes
  • Processing planning - Determine memory requirements
  • Cost estimation - Calculate cloud storage costs

Limitations

  • Calculates uncompressed size only
  • Actual compressed size varies by format and compression
  • Does not account for tile/block overhead
For compressed size estimates:
  • GeoTIFF with LZW: ~30-50% of uncompressed
  • JPEG2000: ~10-20% of uncompressed
  • Cloud-optimized formats: add ~5-10% for overviews

gdal2AsciiLatLonBands.py

Exports raster band data to ASCII/CSV format with optional latitude/longitude or XY coordinate columns.

Usage

Parameters

string
required
Input raster file
string
Output ASCII/CSV file (writes to stdout if not specified)

Options

integers
Extract subset window (offsets and dimensions in meters)
integer
Band number to export (can be specified multiple times for multiple bands). Defaults to band 1.
flag
Add header row with field names
flag
Include Lat/Lon columns (uses GDAL projection to calculate)
flag
Include Y,X columns in meters

Examples

Output Format

Without coordinates:
With header and Y,X:
With Lat/Lon and multiple bands:

Use Cases

  • Point sampling - Extract band values at specific locations
  • Data integration - Create CSV for import to databases or spreadsheets
  • Validation - Compare pixel values across different datasets
  • Analysis input - Generate point clouds for statistical analysis

Common Workflows

Complete Slope Analysis Workflow

Metadata Documentation Workflow

Data Planning Workflow

Installation

Basic Installation

Full Installation (All Utilities)

Verify Installation

Requirements Summary

Troubleshooting

Install GDAL Python bindings:
Ensure:
  • Input file has valid projection
  • Template XML is valid FGDC format
  • Longitude values are in -180 to 180 range
Check:
  • Input file is tab-delimited
  • Contains required columns: level, value, count, cumulative
  • matplotlib backend is properly configured
Verify:
  • Reference image has correct projection
  • Bounds are in correct order (min before max)
  • Resolution units match projection units

Author

Developed by Trent Hare and contributors at USGS Astrogeology Science Center.

License

Public domain (Unlicense) unless otherwise specified.